package attack.nartan;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;
import framework.Util;
import framework.movements.PropulsionMovement;

public class Dash extends SwordStrike {
	
	private static final int DASH_SPEED = 1200;
	
	protected int timeHit;

	public Dash(BasicFighter attacker) {
		super(attacker);
		this.spriteSheet = attacker.getAnimations().getSpriteSheet();
		this.attackAnimation = new Animation();
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(2, 4), 300);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(3, 4), 250);
		this.attackAnimation.addFrame(this.spriteSheet.getSprite(2, 4), 150);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attacker = attacker;
		this.isJumpingAuthorized = true;
	}

	@Override
	public int getDuration() {
		return hasHit() ? timeHit + 100 : 700;
	}

	@Override
	public Rectangle getHitBox() {
		return super.getHitBox();
	}

	@Override
	public int getPower() {
		return 10;
	}
	
	@Override
	public void update(GameContainer gc, int delta) {
		if (this.attackAnimation.getFrame() == 1 || this.attackAnimationBackward.getFrame() == 1) {
			getAttacker().setX(getAttacker().getX() + getStepXSpeed(delta));
		}
		super.update(gc, delta);
	}

	@Override
	public void interrupt() {
		super.interrupt();
	}

	@Override
	public void trigger() {
		super.trigger();
		getAttacker().interruptJump();
		setXspeed(getAttacker().isForward() ? DASH_SPEED : -DASH_SPEED);
		attackAnimationUsed.setAutoUpdate(true);
		timeHit = 0;
	}

	@Override
	public void draw(Graphics g) {
		BasicFighter a = getAttacker();
		this.attackAnimationUsed.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight());
	}
	
	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		if (!hasHit()) {
			timeHit = currentTime;
			if (f.getCurrentAttack().isInterruptable()) {
				f.getCurrentAttack().interrupt();
			}
			int forward = f.isForward() ? -1 : 1;
			int distance = (getDuration() - timeHit) * 1800 / getDuration();
			PropulsionMovement m = new PropulsionMovement(f, distance * forward, -300);
			m.setDuration(200);
			f.move(m);
			f.hit(getPower());
			hasHit = true;

			if (f.isParrying()) {
				f.parry(getPower());
				this.parrySound.play();
			} else {
				f.hit(getPower());
				this.hitSound.play();
			}
		}
	}
	
	
	
	@Override
	public void onRelease() {
	}
}
